package com.mariusz.panels;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import org.jdesktop.swingx.JXTitledPanel;
import org.jdesktop.swingx.JXTable;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.JXTableHeader;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import org.jdesktop.swingx.JXButton;

import com.mariusz.data.dto.Rentier;
import com.mariusz.managers.DataManager;
import com.mariusz.managers.NavigationManager;
import com.mariusz.managers.NavigationManager.Screen;
import com.mariusz.managers.ReportManager;

import java.awt.Font;
import java.io.File;

public class RentiersListPanel extends JXTitledPanel implements ActionListener {
	private JXTable rentitiersTable;
	private JXButton btnNewRentier ;
	private JXButton btnEditRentier;
	private JXButton btnClose;
	private JXButton btnRemoveRentier;
	private JButton btnPrint;
	private JButton btnExport;
	
	public RentiersListPanel() {
			setTitleForeground(new Color(0, 0, 0));
			setTitle("Tenants List");
			getContentContainer().setLayout(null);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(0, 227, 681, 205);
			getContentContainer().add(scrollPane);
			rentitiersTable = new JXTable();
			rentitiersTable.setSortsOnUpdates(false);
			rentitiersTable.setSortable(false);
			rentitiersTable.setColumnControlVisible(true);
			scrollPane.setViewportView(rentitiersTable);
			rentitiersTable.setModel(DataManager.getInstance().getRentiersTableModel());
			
			btnNewRentier = new JXButton();
			btnNewRentier.setFont(new Font("Tahoma", Font.PLAIN, 11));
			btnNewRentier.setText("New Tenant");
			btnNewRentier.setBounds(510, 11, 123, 23);
			btnNewRentier.addActionListener(this);
			getContentContainer().add(btnNewRentier);
			
			btnEditRentier = new JXButton();
			btnEditRentier.setFont(new Font("Tahoma", Font.PLAIN, 11));
			btnEditRentier.setText("Edit Tenant");
			btnEditRentier.setBounds(510, 45, 123, 23);
			btnEditRentier.addActionListener(this);
			getContentContainer().add(btnEditRentier);
			
			btnClose = new JXButton();
			btnClose.setFont(new Font("Tahoma", Font.PLAIN, 11));
			btnClose.setText("Close");
			btnClose.setBounds(510, 193, 123, 23);
			btnClose.addActionListener(this);
			getContentContainer().add(btnClose);
			
			ResourceImagePanel panel = new ResourceImagePanel("Wordsworths-House-small.JPG");
			panel.setBounds(0, 0, 500, 216);
			getContentContainer().add(panel);
			
			btnRemoveRentier = new JXButton();
			btnRemoveRentier.setText("Remove Tenant");
			btnRemoveRentier.setFont(new Font("Tahoma", Font.PLAIN, 11));
			btnRemoveRentier.setBounds(510, 79, 123, 23);
			btnRemoveRentier.addActionListener(this);
			getContentContainer().add(btnRemoveRentier);

			btnPrint = new JButton("Print Payments");
			btnPrint.setBounds(510, 130, 123, 23);
			btnPrint.addActionListener(this);
			getContentContainer().add(btnPrint);
			
			btnExport = new JButton("Export Payments");
			btnExport.setBounds(510, 159, 123, 23);
			btnExport.addActionListener(this);
			getContentContainer().add(btnExport);
		}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnEditRentier){
			int selected = rentitiersTable.getSelectedRow();
			if(selected <0){
				return;
			}
			Rentier r = DataManager.getInstance().getRentiers().get(selected);
			DataManager.getInstance().setCurrentRentier(r);
			NavigationManager.getInstance().switchTo(Screen.RENTIER_DETAILS);
		}else if(e.getSource() == btnNewRentier){
			Rentier rentier = new Rentier();
			DataManager.getInstance().setCurrentRentier(rentier);
			NavigationManager.getInstance().switchTo(Screen.RENTIER_DETAILS);
		}else if(e.getSource() == btnClose){
			NavigationManager.getInstance().switchTo(Screen.STATUS_PANEL);
			
		}else if(e.getSource() == btnRemoveRentier){
			int row = rentitiersTable.getSelectedRow();
			if(row >=0){
				Rentier r =DataManager.getInstance().getRentiers().get(row);
				int answer = JOptionPane.showConfirmDialog(this, "Are you really sure you want to delate "+r.getName()+"?");
			    if (answer == JOptionPane.YES_OPTION) {
			    	DataManager.getInstance().getRentiers().remove(row);
					this.show(true);
			    } else if (answer == JOptionPane.NO_OPTION) {
			      // User clicked NO.
			    }
				
			}
		}else if(e.getSource() == btnPrint){
			int selected = rentitiersTable.getSelectedRow();
			if(selected <0){
				return;
			}
			Rentier rentier = DataManager.getInstance().getRentiers().get(selected);
			
			ReportManager.getInstance().printPaymentReport(rentier);
		
		}else if(e.getSource() == btnExport){
			int selected = rentitiersTable.getSelectedRow();
			if(selected <0){
				return;
			}
			Rentier rentier = DataManager.getInstance().getRentiers().get(selected);;
		
				JFileChooser fileChooser = new JFileChooser("." );
			fileChooser.setSelectedFile(new File(rentier.getName()+"-payments.pdf"));
				int returnVal = fileChooser.showSaveDialog(this);
				
			    if(returnVal == JFileChooser.APPROVE_OPTION) {
			           String filename = fileChooser.getSelectedFile().getName();
			           ReportManager.getInstance().savePaymentReport(rentier, filename);
			    }
			
		}
	}
	@Override
	public void show(boolean b) {
		rentitiersTable.setModel(DataManager.getInstance().getRentiersTableModel());
		super.show(b);
	}
}


